mat = 0
async def write_data(user, mat):
users = await read_data()
if str(user) in users:
return False
with open("match.json", "w") as f:
json.dump(users, f)
return Ture
@bot.command(pass_context = True)
async def rand(ctx):
users, mat = await write_data(ctx.author, mat = 0)
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
await ctx.send("本日次數已用完")
if mat == 1:
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
else:
await ctx.send("本日次數已用完")
# user = author,但為甚麼要加上ctx呢,因為姑且算是文字吧XD
async def write_data(user, mat):
# 省略
return Ture
其實應該要回傳2個東西回去,但是這邊只有一個,所以需要更改
我們需要知道在json裡面的key and value,但這邊要先設想一個情況,如果key根本沒在json裡怎麼辦
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
if str(user.id) not in users :
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
mat = 1
users[str(user.id)]["count"] = 0
mat = 1
if users[str(user.id)]["count"] == 0:
users[str(user.id)] = {}
users[str(user.id)]["count"] = 1
mat = 1
return True, mat
async def match_data(user, mat):
users = await read_data()
if mat == 1:
mat = 2
return True, mat
from discord.ext import commands
import discord
from discord.ext.commands import bot
from core.any import Cog_Extension
import json
import random
lucky = ["lucky~~~", "not bad", "Soso", "green hat",
"fuck your self"]
class randomLucky(Cog_Extension):
@commands.command()
async def rand(self,ctx):
users, mat = await write_data(ctx.author, mat = 0)
if mat == 1:
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
else:
await ctx.send("本日次數已用完")
async def read_data():
with open("match.json", "r") as file:
users = json.load(file)
return users
async def write_data(user, mat):
users = await read_data()
if str(user) in users:
return False
#find json.key
if str(user.id) not in users :
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
mat = 1
if users[str(user.id)]["count"] == 0:
users[str(user.id)] = {}
users[str(user.id)]["count"] = 1
mat = 1
with open("match.json", "w") as f:
json.dump(users, f)
return True, mat
async def match_data(user, mat):
users = await read_data()
if mat == 1:
mat = 2
return True, mat
def setup(bot):
bot.add_cog(randomLucky(bot))